package com.azusa.ktc_friend.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.azusa.ktc_friend.client.UserClient;
import com.azusa.ktc_friend.dao.NoFriendDao;
import com.azusa.ktc_friend.entity.NoFriend;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import utils.IdWorker;
import com.azusa.ktc_friend.dao.FriendDao;
import com.azusa.ktc_friend.entity.Friend;

/**
 * @Description tb_friend 服务层
 * @author Azusa
 * @date 2020-10-12 18:26:41
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class FriendServiceImpl implements FriendService{

	@Autowired
	private FriendDao friendDao;

	@Autowired
	private IdWorker idWorker;

	@Autowired
	private UserClient userClient;

	@Autowired
	private NoFriendDao noFriendDao;


	@Override
	@Transactional/*(propagation = Propagation.REQUIRES_NEW)*/	//声明事务传播行为
	public boolean addLikeFriend(Friend friend) {
		//判断是否已经有关注，有则取消关注，同时设置friendId的isLike为0
		if (friendDao.countByUseridAndFriendid(friend.getUserid(),friend.getFriendid()) > 0){
			//删除关注
			friendDao.deleteByUseridAndFriendid(friend.getUserid(),friend.getFriendid());
			//设置被取消关注用户的isLike为0
			friendDao.updateLikeByUserIdAndFriendId(friend.getFriendid(),friend.getUserid(),"0");
			//关注数-1
			userClient.updateFollowCount(friend.getUserid(),-1);
			//对方粉丝数-1
			userClient.updateFansCount(friend.getFriendid(),-1);
			return false;
		}
		//查询是否被关注用户是否有关注
		if (friendDao.countByUseridAndFriendid(friend.getFriendid(),friend.getUserid()) > 0){
			//修改被关注用户的isLike为1
			friendDao.updateLikeByUserIdAndFriendId(friend.getFriendid(),friend.getUserid(),"1");
			friend.setIslike("1");
		}else {
			//如果没互关则设置添加用户的isLike为0
			friend.setIslike("0");
		}
		//删除拉黑记录
		noFriendDao.deleteByUseridAndFriendid(friend.getUserid(),friend.getFriendid());
		//添加关注记录
		friendDao.save(friend);
		//关注数+1
		Result r1 = userClient.updateFollowCount(friend.getUserid(), 1);//当前事务没采用分布式事务进行管理，如果出现异常也会进行往下执行
		// 对方粉丝数+1
		Result r2 = userClient.updateFansCount(friend.getFriendid(), 1);
		return true;
	}

	@Override
	@Transactional
	public boolean deleteFriend(Friend friend) {
		//查看先前是否有拉黑
		if (noFriendDao.countByUseridAndFriendid(friend.getUserid(),friend.getFriendid()) > 0){
			noFriendDao.deleteByUseridAndFriendid(friend.getUserid(),friend.getFriendid());
			return false;
		}
		//判断先前是否有关注，有则删除
		if (friendDao.countByUseridAndFriendid(friend.getUserid(),friend.getFriendid()) > 0){
			//删除关注
			friendDao.deleteByUseridAndFriendid(friend.getUserid(),friend.getFriendid());
			//设置被取消关注用户的isLike为0
			friendDao.updateLikeByUserIdAndFriendId(friend.getFriendid(),friend.getUserid(),"0");
			//关注数-1
			userClient.updateFollowCount(friend.getUserid(),-1);
			//对方粉丝数-1
			userClient.updateFansCount(friend.getFriendid(),-1);
		}
		noFriendDao.save(new NoFriend(friend.getUserid(),friend.getFriendid()));
		return true;
	}



}
